package com.zking.frame.czp;

import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import com.zking.db.BaseDao;

public class ARAP_Jf extends JFrame {

	{
		BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
		try {
			org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	private JRadioButton All_Receivables, All_Payables, All_ReceivablesAndPayables;
	private JButton Select, Add, Receivables, Payables, Export;
	private JCheckBox NoWriteOff, WriteOff;
	private JPanel FunctionPanel;
	private JLabel dateJLabel, selectJLabel;
	private JTextField dateJT1, dateJT2;
	private JTextField selectField;
	private JButton selectButton;
	private List<String> lists = new ArrayList<String>();// 子面板的内容
	private List<String> addslists = new ArrayList<String>();// 接收字面板的参数重新填入面板
	String[] columns = { "客户名称", "款项名称", "应收金额", "已收金额", "剩余欠款", "经办人", "日期", "销账", "备注" };
	private int rowCount;// 数据库行
	private DefaultTableModel dtm;
	private int columnCount;// 数据库列
	private JScrollPane sp;// 滚动面板
	private String[][] str;// 数据
	private List<Object> params;

	private JTable table;

//获取今年去年时间
	LocalDate today = LocalDate.now();
	int Years = -1;
	LocalDate lastYear = today.plus(Years, ChronoUnit.YEARS);

	private void jButtonActionPerformed(java.awt.event.ActionEvent evt) {
		// TODO add your handling code here:导出
		FileDialog fd = new FileDialog(this, "另存为", FileDialog.SAVE);
		fd.setLocation(500, 350);
		fd.setVisible(true);
		String stringfile = fd.getDirectory() + fd.getFile() + ".xls";
		try {
			ExcelExporter export = new ExcelExporter();
			export.exportTable(table, new File(stringfile));
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public ARAP_Jf() {

		Container c = getContentPane();
		setSize(1154, 738);
		c.setLayout(null);

		JPanel FunctionPanel = new JPanel();
		FunctionPanel.setLayout(null);
		FunctionPanel.setBounds(0, 0, 1154, 100);
		FunctionPanel.setBackground(new Color(185, 200, 200));

		JButton Add = new JButton("增加(A)");
		Add.setBounds(934, 9, 91, 29);
		FunctionPanel.add(Add);

		Add.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				new MemberInfo(ARAP_Jf.this);
			}
		});

		JButton Export = new JButton("导出(E)");
		Export.setBounds(1039, 9, 91, 29);
		FunctionPanel.add(Export);

		JButton Receivables = new JButton("收款(S)");
		Receivables.setBounds(934, 58, 91, 29);
		FunctionPanel.add(Receivables);

		JButton Payables = new JButton("付款(P)");
		Payables.setBounds(1039, 58, 91, 29);
		FunctionPanel.add(Payables);

		JButton Select = new JButton("查询(P)");
		Select.setBounds(815, 16, 94, 73);
		FunctionPanel.add(Select);

		JRadioButton All_Receivables = new JRadioButton("所有应收款");
		All_Receivables.setBounds(35, 20, 110, 21);
		FunctionPanel.add(All_Receivables);

		JRadioButton All_Payables = new JRadioButton("所有应付款");
		All_Payables.setBounds(165, 20, 110, 21);
		FunctionPanel.add(All_Payables);

		JRadioButton All_ReceivablesAndPayables = new JRadioButton("所有应收应付");
		All_ReceivablesAndPayables.setBounds(295, 20, 135, 21);
		FunctionPanel.add(All_ReceivablesAndPayables);

		JCheckBox NoWriteOff = new JCheckBox("未注销");
		NoWriteOff.setBounds(104, 61, 80, 21);
		FunctionPanel.add(NoWriteOff);

		JCheckBox WriteOff = new JCheckBox("已注销");
		WriteOff.setBounds(233, 61, 80, 21);
		FunctionPanel.add(WriteOff);
		JLabel dateJLabel = new JLabel("日期从");
		dateJLabel.setBounds(444, 17, 80, 21);
		FunctionPanel.add(dateJLabel);
//日历控件

//获得今年和去年的时间		

		dateJT1 = new JTextField("" + lastYear);
		dateJT1.setBounds(500, 17, 120, 21);
		FunctionPanel.add(dateJT1);
		CalendarPanel p1 = new CalendarPanel(dateJT1, "yyyy/MM/dd");
		p1.initCalendarPanel();
		c.add(p1);
//标签
		JLabel daJLabel2 = new JLabel("至");
		daJLabel2.setBounds(624, 17, 100, 21);
		FunctionPanel.add(daJLabel2);

		dateJT2 = new JTextField("" + today);
		dateJT2.setBounds(643, 17, 120, 21);
		FunctionPanel.add(dateJT2);
		CalendarPanel p2 = new CalendarPanel(dateJT2, "yyyy/MM/dd");
		p2.initCalendarPanel();
		c.add(p2);

		selectJLabel = new JLabel("名称||金额||经办人||备注：");
		selectJLabel.setBounds(435, 43, 300, 21);
		FunctionPanel.add(selectJLabel);
		selectButton = new JButton("查");
		selectButton.setBounds(736, 66, 50, 28);
		FunctionPanel.add(selectButton);
		selectButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				CustomerMsgson test = new CustomerMsgson(null, true);
				if (!test.prename.equals(null)) {
					selectField.setText(test.prename);
					FunctionPanel.add(selectField);
				}
			}
		});

		selectField = new JTextField();
		selectField.setBounds(434, 66, 300, 28);

		FunctionPanel.add(selectField);
		c.add(FunctionPanel);

//表格

		dtm = new DefaultTableModel(str, columns);
		table = new JTable(dtm) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;// 表格不允许编制
			}
		};
		// 表头
		JTableHeader head = table.getTableHeader();
		// 表头颜色设置
		Color color_tableHeader = new Color(222, 231, 239);
		head.setBackground(color_tableHeader);
		// 设置表头大小
		head.setPreferredSize(new Dimension(head.getWidth(), 40));
		// 设置每个格子的高度
		table.setRowHeight(30);
		// 表格添加到滚动面板
		sp = new JScrollPane(table);
		sp.setBounds(0, 100, 1154, 595);
		// 水平
		sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		c.add(sp);

//导出表
		Export.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				jButtonActionPerformed(e);
			}
		});

		setResizable(false);
		setVisible(true);
		setLocationRelativeTo(null);
		setTitle("应收应付");
		setIconImage(Toolkit.getDefaultToolkit().createImage(Register_Jf.class.getResource("应收应付界面左上角.png")));
	}

	public class MemberInfo extends JDialog {

		private static final long serialVersionUID = 1L;
		private JPanel pl_center;// 大面板
		private JLabel lb_Name;// 书名
		private JTextField tf_Namel;
		private JLabel lb_Money;// 作者
		private JTextField tf_Money;
		private JLabel lb_amount;// 卖价
		private JTextField tf_amount;
		private JLabel lb_Incharge;// 入库数量
		private JTextField tf_Incharge;
		private JLabel lb_remarks;// 书架号
		private JTextField tf_remarks;
		private JTextField daTextField;
		private JLabel dateJLabel, typJLabel;
		private JTextField PcJTextField;
		private JButton customButton;

		private JComboBox<String> typeBox;

		private JButton bt_sure;
		private JButton bt_cancel;
		private String id;

		// 实例类

		public MemberInfo(Frame owner) {
			super(owner, true);
			Container c2 = getContentPane();
			c2.setLayout(null);
			c2.setBackground(new Color(192, 192, 192));

			// 大面板
			pl_center = new JPanel();
			pl_center.setBounds(20, 22, 532, 236);
			pl_center.setBackground(new Color(0xE0EEEE));
			pl_center.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.gray));
			pl_center.setLayout(null);
			c2.add(pl_center);

			// name
			lb_Name = new JLabel("客户名称：");
			lb_Name.setFont(new Font("楷体", Font.BOLD, 18));
			lb_Name.setBounds(18, 15, 167, 15);
			pl_center.add(lb_Name);

			// 增加客户
			customButton = new JButton("...");
			customButton.setBounds(470, 10, 50, 23);
			pl_center.add(customButton);
			customButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					CustomerMsgson test = new CustomerMsgson(null, true);
					if (!test.prename.equals(null)) {
						tf_Namel.setText(test.prename);
						pl_center.add(tf_Namel);
						id = test.preid;
						lists.add(id);

					}
				}
			});

			// 客户名称输入框
			tf_Namel = new JTextField();
			tf_Namel.setBackground(new Color(0xFCFCFC));
			tf_Namel.setFont(new Font("宋体", Font.PLAIN, 18));
			tf_Namel.setBounds(110, 10, 360, 23);
			tf_Namel.setEditable(false);
			pl_center.add(tf_Namel);

			tf_Namel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.gray));

			// 款项名称
			lb_Money = new JLabel("款项名称：");
			lb_Money.setFont(new Font("楷体", Font.BOLD, 18));
			lb_Money.setBounds(18, 62, 167, 15);
			pl_center.add(lb_Money);

			// 款项名称输入框
			tf_Money = new JTextField(30);
			tf_Money.setBackground(new Color(0xFCFCFC));
			tf_Money.setFont(new Font("宋体", Font.PLAIN, 18));
			tf_Money.setBounds(110, 57, 389, 23);
			pl_center.add(tf_Money);

			tf_Money.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.gray));

			// 金额
			lb_amount = new JLabel("金额：    ");
			lb_amount.setFont(new Font("楷体", Font.BOLD, 18));
			lb_amount.setBounds(18, 106, 167, 18);
			pl_center.add(lb_amount);

			// 输入框
			tf_amount = new JTextField(30);
			tf_amount.setBackground(new Color(0xFCFCFC));
			tf_amount.setFont(new Font("宋体", Font.PLAIN, 18));
			tf_amount.setBounds(110, 100, 190, 23);
			pl_center.add(tf_amount);
			tf_amount.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.gray));

			// 类型标签
			typJLabel = new JLabel("类型");
			typJLabel.setBounds(344, 100, 100, 23);
			typJLabel.setFont(new Font("楷体", Font.BOLD, 18));
			pl_center.add(typJLabel);

			// 类型框
			typeBox = new JComboBox<String>();
			typeBox.addItem("应收");
			typeBox.addItem("已收");
			typeBox.setBounds(403, 102, 120, 23);
			pl_center.add(typeBox);

//日期标签			
			dateJLabel = new JLabel("日期：");
			dateJLabel.setFont(new Font("楷体", Font.BOLD, 18));
			dateJLabel.setBounds(341, 150, 163, 23);
			pl_center.add(dateJLabel);

//日期框			
			daTextField = new JTextField("" + today);
			daTextField.setBounds(402, 150, 120, 23);
			daTextField.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.gray));
			pl_center.add(daTextField);

			// 经办人
			lb_Incharge = new JLabel("经办人  : ");
			lb_Incharge.setFont(new Font("楷体", Font.BOLD, 18));
			lb_Incharge.setBounds(19, 150, 150, 18);
			pl_center.add(lb_Incharge);

			// 经办人输入框
			tf_Incharge = new JTextField(30);
			tf_Incharge.setBackground(new Color(0xFCFCFC));
			tf_Incharge.setFont(new Font("宋体", Font.PLAIN, 18));
			tf_Incharge.setBounds(110, 150, 190, 23);
			tf_Incharge.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.gray));
			pl_center.add(tf_Incharge);

			// 备注
			lb_remarks = new JLabel("备注：    ");
			lb_remarks.setFont(new Font("楷体", Font.BOLD, 18));
			lb_remarks.setBounds(19, 188, 130, 23);
			pl_center.add(lb_remarks);

			// 备注输入框
			tf_remarks = new JTextField(30);
			tf_remarks.setBackground(new Color(0xFCFCFC));
			tf_remarks.setFont(new Font("宋体", Font.PLAIN, 18));
			tf_remarks.setBounds(110, 188, 389, 23);
			tf_remarks.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.white, Color.gray));
			pl_center.add(tf_remarks);

//获取输入框信息
			String sql_Money = tf_Money.getText();
			String sql_amount = tf_amount.getText();
			String sql_Incharge = tf_Incharge.getText();
			String sql_remarks = tf_remarks.getText();

			// 底部
			// 确定
			bt_sure = new JButton("确定");
			bt_sure.setBounds(181, 275, 80, 30);
			bt_sure.setFont(new Font("楷体", Font.BOLD, 15));
			bt_sure.setBackground(new Color(0xF0F0F0));
			bt_sure.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			bt_sure.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			bt_sure.addActionListener(e -> {

				String typemount = typeBox.getSelectedItem().toString();
				// System.out.println(typemount);
				String[] rowData = new String[columns.length];// 一行的数目

//				// lists.add(tf_Namel.getText());
//				lists.add(tf_Money.getText());
//				lists.add(tf_amount.getText());
//				lists.add(tf_Incharge.getText());
//				lists.add(daTextField.getText());
//				lists.add(tf_remarks.getText());
				if (typemount.equals("应收")) {
					List arraylist1 = new ArrayList();
					arraylist1.add(tf_Namel.getText());
					arraylist1.add(tf_Money.getText());
					arraylist1.add(tf_amount.getText());
					arraylist1.add(0);
					arraylist1.add(tf_amount.getText());
					arraylist1.add(tf_Incharge.getText());
					arraylist1.add(daTextField.getText());
					arraylist1.add("否");
					arraylist1.add(tf_remarks.getText());
					Vector v1 = new Vector();
					v1.add(arraylist1.get(0));
					v1.add(arraylist1.get(1));
					v1.add(arraylist1.get(2));
					v1.add(arraylist1.get(3));
					v1.add(arraylist1.get(4));
					v1.add(arraylist1.get(5));
					v1.add(arraylist1.get(6));
					v1.add(arraylist1.get(7));
					v1.add(arraylist1.get(8));
					dtm.addRow(v1);

				} else {
					List arraylist1 = new ArrayList();
					arraylist1.add(tf_Namel.getText());
					arraylist1.add(tf_Money.getText());
					arraylist1.add(0);
					arraylist1.add(tf_amount.getText());
					arraylist1.add("-" + tf_amount.getText());
					arraylist1.add(tf_Incharge.getText());
					arraylist1.add(daTextField.getText());
					arraylist1.add("否");
					arraylist1.add(tf_remarks.getText());
					Vector v1 = new Vector();
					v1.add(arraylist1.get(0));
					v1.add(arraylist1.get(1));
					v1.add(arraylist1.get(2));
					v1.add(arraylist1.get(3));
					v1.add(arraylist1.get(4));
					v1.add(arraylist1.get(5));
					v1.add(arraylist1.get(6));
					v1.add(arraylist1.get(7));
					v1.add(arraylist1.get(8));
					dtm.addRow(v1);

				}

//				params = new ArrayList<Object>();
//				for (int i = 0; i < lists.size(); i++) {
//					String data = lists.get(i);
//					if (data == null || "".equals(data) || data.length() == 0) {
//						params.add(null);
//					} else {
//						params.add(data);
//					}
//				}
//				// 数据库操作
//				BaseDao base = new BaseDao();
//				if (typemount.equals("应收")) {
//					String sql = "insert into arap(" + "id,ofMoney,amountReceivable,personInCharge,date,"
//							+ "remarks) values(" + "?,?,?,?,?," + "?)";
//					System.out.println(lists);
//					try {
//						// 转化成数组
//						base.update(sql, params.toArray());
//					} catch (SQLException e1) {
//						e1.printStackTrace();
//					} finally {
//						try {
//							base.closeConnection();
//						} catch (SQLException e1) {
//							e1.printStackTrace();
//						}
//					}
//				} else {
//
//					String sql = "insert into arap(" + "id,ofMoney,amountReceived,personInCharge,date,"
//							+ "remarks) values(" + "?,?,?,?,?," + "?)";
//					System.out.println(2);
//					try {
//						// 转化成数组
//						base.update(sql, params.toArray());
//					} catch (SQLException e1) {
//						e1.printStackTrace();
//					} finally {
//						try {
//							base.closeConnection();
//						} catch (SQLException e1) {
//							e1.printStackTrace();
//						}
//					}
//
//				}
//				exchangeData();
//				rowData[0] = str[rowCount - 1][0];
//				// 将list转化成数组添加道主面板
//				rowData[1] = lists.get(0);
//				rowData[2] = lists.get(1);
//				rowData[3] = lists.get(2);
//				rowData[4] = lists.get(3);
//				rowData[6] = lists.get(4);
//				rowData[5] = lists.get(5);
//
//				dtm.addRow(rowData);// 添加到后面一行

//

				dispose();

			});
			c2.add(bt_sure);
			// 取消
			bt_cancel = new JButton("取消");
			bt_cancel.setBounds(301, 275, 80, 30);
			bt_cancel.setFont(new Font("楷体", Font.BOLD, 15));
			bt_cancel.setBackground(new Color(0xF0F0F0));
			bt_cancel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			bt_cancel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			bt_cancel.addActionListener(e -> {
				dispose();
			});
			c2.add(bt_cancel);

			setTitle("款项增加");
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			setSize(594, 365);
			setLocationRelativeTo(null);
			setVisible(true);

		}

	}

	/**
	 * @param定义数组的大小 @param把结果集中的每一行数据都转成一个对象数组object[] @param将list==>数组String
	 *               str[][]
	 */
	public void exchangeData() {
		BaseDao base = new BaseDao();
		// 查询语句
		String sql = "select * from arap";
		// 数据集合
		List<Object[]> data = null;
		try {
			data = base.queryArrayListHandler(sql);
			// 获取数据库行和列
			rowCount = data.size();
			columnCount = data.get(0).length;
			// 设置为数组大小
			str = new String[rowCount][columnCount];
			// 将list中的集合装化成数组String str[]
			for (int i = 0; i < data.size(); i++) {
				for (int j = 0; j < data.get(i).length; j++)
					if (data.get(i)[j] != null) {
						str[i][j] = data.get(i)[j] + "";
					} else {
						str[i][j] = "";
					}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				// 关闭连接
				base.closeConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @param定义数组的大小 @param把结果集中的每一行数据都转成一个对象数组object[] @param将list==>数组String
	 *               str1[]
	 */
//	public String[] updateData(int selcetId) {
//		// 设置为数组大小
//		String[] str1 = new String[14];
//		BaseDao base = new BaseDao();
//		// 查询语句
//		String sql = "select name,pinYinCode,city,address,discount," + "isVip,bankOfDeposit,tel,postalCode,fax,"
//				+ "e_mail,contactPerson,contactTel from client where id=" + selcetId;
//		// 数据集合
//		Object[] data = null;
//		try {
//			data = base.queryArrayHandler(sql);
//			// 获取数据库行和列
//			rowCount = data.length;
//			for (int i = 0; i < data.length; i++) {
//				if (data[i] != null) {
//					str1[i] = data[i] + "";
//				} else {
//					str1[i] = "";
//				}
//			}
//		} catch (SQLException e) {
//			e.printStackTrace();
//		} finally {
//			try {
//				// 关闭连接
//				base.closeConnection();
//			} catch (SQLException e) {
//				e.printStackTrace();
//			}
//		}
//		return str1;
//	}

	public static void main(String[] args) {
		new ARAP_Jf();
	}
}